package com.atguigu.datastream.test.day06;

import com.atguigu.datastream.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.*;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * ClassName: Flink01_Flink_Customer_WaterMark
 * Package: com.atguigu.test.day06
 * Description:
 *            有序流创建水位线
 * @Author ChenJun
 * @Create 2023/4/12 20:22
 * @Version 1.0
 */
public class Flink01_Flink_Customer_WaterMark {
    public static void main(String[] args) throws Exception {

        //1. 创建流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


        //TODO 设置WaterMark周期生成时间
//        env.getConfig().setAutoWatermarkInterval(1000);

        env.setParallelism(1);

        //2. 从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);


        //3. 将数据转换为JavaBean
        SingleOutputStreamOperator<WaterSensor> map = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //TODO 4.指定WaterMark以及事件时间戳   使用有序流中的WaterMark
        SingleOutputStreamOperator<WaterSensor> streamOperator = map.assignTimestampsAndWatermarks(new WatermarkStrategy<WaterSensor>() {
            @Override
            public WatermarkGenerator<WaterSensor> createWatermarkGenerator(WatermarkGeneratorSupplier.Context context) {
                return new MyWatermarkGenerator(Duration.ofSeconds(3));
            }
        }
                .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                    @Override
                    public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                        return element.getTs() * 1000;
                    }
                }));

        //5. 将相同的key聚合到一起
        KeyedStream<WaterSensor, Tuple> keyedStream = streamOperator.keyBy("id");

        // TODO 6.开启一个基于事件时间的滚动窗口，窗口大小为5S
        WindowedStream<WaterSensor, Tuple, TimeWindow> window = keyedStream.window(TumblingEventTimeWindows.of(Time.seconds(5)));

        SingleOutputStreamOperator<String> process = window.process(new ProcessWindowFunction<WaterSensor, String, Tuple, TimeWindow>() {
            @Override
            public void process(Tuple tuple, ProcessWindowFunction<WaterSensor, String, Tuple, TimeWindow>.Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                String msg =
                        "窗口: [" + context.window().getStart() / 1000 + "," + context.window().getEnd() / 1000 + ") 一共有 "
                                + elements.spliterator().estimateSize() + "条数据 ";
                out.collect(msg);
            }
        });

        process.print();
        SingleOutputStreamOperator<WaterSensor> vc = window.sum("vc");

        vc.print();

        // 执行
        env.execute();
        }

        // TODO 自定义一个类实现WatermarkGenerator这个接口
      public static class MyWatermarkGenerator implements WatermarkGenerator<WaterSensor>{

            //当前最大的时间戳
            private long maxTimestamp;

            //乱序程度（延迟时间）
            private long outOfOrdernessMillis;

            public MyWatermarkGenerator(Duration maxOutOfOrderness) {
                //获取传入的乱序程度
                this.outOfOrdernessMillis = maxOutOfOrderness.toMillis();

                // 起始WaterMark的值为Long的最小值
                this.maxTimestamp = Long.MIN_VALUE + outOfOrdernessMillis + 1;
            }


            /**
             * 每来一条数据调用一次 可以生成WaterMark
             * @param event
             * @param eventTimestamp
             * @param output
             */
            @Override
            public void onEvent(WaterSensor event, long eventTimestamp, WatermarkOutput output) {
                maxTimestamp = Math.max(maxTimestamp, eventTimestamp);
                System.out.println("生成WaterMark："+(maxTimestamp - outOfOrdernessMillis - 1));
                output.emitWatermark(new Watermark(maxTimestamp - outOfOrdernessMillis - 1));
            }


            /**
             * 默认每隔200ms 调用一次 可以生成WaterMark
             * @param output
             */
            @Override
            public void onPeriodicEmit(WatermarkOutput output) {
//                System.out.println("生成WaterMark："+(maxTimestamp - outOfOrdernessMillis - 1));
//                output.emitWatermark(new Watermark(maxTimestamp - outOfOrdernessMillis - 1));
            }
        }
}
